package com.eagle.coders.core.exceptions;

import java.util.Map;

/**
 * 
 * @author Anees
 *
 */
public class BaseAppException extends Exception {

	private static final long serialVersionUID = 1L;

	private boolean isLogged = false;
    private String errorMessageLocation = null; // where to find error message properties file (resource bundle)
    private String messageKey = null; // key to identify which error message in the resource bundle
    private String errorCode = null; // code to identify each types of error
    private String module = null; // code of the module where the error occurs
    private String subModule = null; // code of the sub module where the error occurs
    private Object errorData = null; // the data that causes error
    private Map<String, String> baseAppErrorMessageMap;
    /*
     * This argument array is used in front-end for displaying proper concatenated
     * message to user, using pattern (retrieved from resource bundle) compatible 
     * with java.text.MessageFormat.
     */
    private Object[] messageArgs = null;

    //=============================================================================================
    // Constructors
    //=============================================================================================

    public BaseAppException() {
    }

    public BaseAppException(String msg) {
        super(msg);
    }

    public BaseAppException(String msg, Throwable cause) {
        super(msg, cause);
    }

    public BaseAppException(Throwable cause) {
        super(cause);
    }
    
    public BaseAppException(String msg, Map<String, String> errors){
    	super(msg);
    	this.baseAppErrorMessageMap = errors;
    }

    //---------------------------------------------------------------------------------------------
    // Below are similar set of constructors, but with the specific attributes and its
    // corresponding pattern arguments for user-aware message purpose.

    public BaseAppException(String msg, String errorMessageLocation, String messageKey,
            String errorCode, String module, String subModule, Object[] msgArgs) {
        super(msg);
        this.errorMessageLocation = errorMessageLocation;
        this.messageKey = messageKey;
        this.messageArgs = msgArgs;
        this.errorCode = errorCode;
        this.module = module;
        this.subModule = subModule;
    }

    public BaseAppException(String msg, Throwable cause, String errorMessageLocation,
            String messageKey, String errorCode, String module, String subModule, Object[] msgArgs) {
        super(msg, cause);
        this.errorMessageLocation = errorMessageLocation;
        this.messageKey = messageKey;
        this.messageArgs = msgArgs;
        this.errorCode = errorCode;
        this.module = module;
        this.subModule = subModule;
    }

    public BaseAppException(String msg, String errorMessageLocation, String errorCode,
            String module, String subModule, Object[] msgArgs) {
        super(msg);
        this.errorMessageLocation = errorMessageLocation;
        this.messageKey = errorCode;
        this.messageArgs = msgArgs;
        this.errorCode = errorCode;
        this.module = module;
        this.subModule = subModule;
    }

    public BaseAppException(String msg, Throwable cause, String errorMessageLocation,
            String errorCode, String module, String subModule, Object[] msgArgs) {
        super(msg, cause);
        this.errorMessageLocation = errorMessageLocation;
        this.messageKey = errorCode;
        this.messageArgs = msgArgs;
        this.errorCode = errorCode;
        this.module = module;
        this.subModule = subModule;
    }

    public BaseAppException(Throwable cause, String errorMessageLocation, String messageKey,
            String errorCode, String module, String subModule, Object[] msgArgs) {
        super(cause);
        this.errorMessageLocation = errorMessageLocation;
        this.messageKey = messageKey;
        this.messageArgs = msgArgs;
        this.errorCode = errorCode;
        this.module = module;
        this.subModule = subModule;
    }

    /*
     * with error data attribute
     */
    public BaseAppException(String errorMessageLocation, String messageKey, String errorCode,
            String module, String subModule, Object errorData, Object[] msgArgs) {
        this.errorMessageLocation = errorMessageLocation;
        this.messageKey = messageKey;
        this.messageArgs = msgArgs;
        this.errorCode = errorCode;
        this.module = module;
        this.subModule = subModule;
        this.errorData = errorData;
    }

    public BaseAppException(String msg, String errorMessageLocation, String messageKey,
            String errorCode, String module, String subModule, Object errorData, Object[] msgArgs) {
        super(msg);
        this.errorMessageLocation = errorMessageLocation;
        this.messageKey = messageKey;
        this.messageArgs = msgArgs;
        this.errorCode = errorCode;
        this.module = module;
        this.subModule = subModule;
        this.errorData = errorData;
    }

    public BaseAppException(String msg, Throwable cause, String errorMessageLocation,
            String messageKey, String errorCode, String module, String subModule, Object errorData,
            Object[] msgArgs) {
        super(msg, cause);
        this.errorMessageLocation = errorMessageLocation;
        this.messageKey = messageKey;
        this.messageArgs = msgArgs;
        this.errorCode = errorCode;
        this.module = module;
        this.subModule = subModule;
        this.errorData = errorData;
    }

    public BaseAppException(Throwable cause, String errorMessageLocation, String messageKey,
            String errorCode, String module, String subModule, Object errorData, Object[] msgArgs) {
        super(cause);
        this.errorMessageLocation = errorMessageLocation;
        this.messageKey = messageKey;
        this.messageArgs = msgArgs;
        this.errorCode = errorCode;
        this.module = module;
        this.subModule = subModule;
        this.errorData = errorData;
    }

    //
    //    /**
    //     * @deprecated  Please use {@link #BaseAppException(String msg, Throwable cause, String errorMessageLocation,
    //     *      String messageKey, String errorCode, Object[] msgArgs)} instead.
    //     */
    //    public BaseAppException(String msg, Throwable cause, String errorMessageLocation,
    //            String messageKey, Object[] msgArgs) {
    //        super(msg, cause);
    //        this.errorMessageLocation = errorMessageLocation;
    //        this.messageKey = messageKey;
    //        this.messageArgs = msgArgs;
    //    }
    //
    //    /**
    //     * @deprecated
    //     */
    //    public BaseAppException(Throwable cause, String errorMessageLocation, String messageKey, 
    //            Object[] msgArgs) {
    //        super(cause);
    //        this.errorMessageLocation = errorMessageLocation;
    //        this.messageKey = messageKey;
    //        this.messageArgs = msgArgs;
    //    }

    //-------------------------------------------------------------------------

    public void setLogged() {
        isLogged = true;
    }

    public boolean isLogged() {
        return isLogged;
    }

    public String getErrorMessageLocation() {
        return errorMessageLocation;
    }

    public String getMessageKey() {
        return messageKey;
    }

    public Object[] getMessageArguments() {
        return messageArgs;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public Object getErrorData() {
        return errorData;
    }

    public String getModule() {
        return module;
    }

    public String getSubModule() {
        return subModule;
    }

	/**
	 * @return the baseAppErrorMessageMap
	 */
	public Map<String, String> getBaseAppErrorMessageMap() {
		return baseAppErrorMessageMap;
	}

	/**
	 * @param baseAppErrorMessageMap the baseAppErrorMessageMap to set
	 */
	public void setBaseAppErrorMessageMap(Map<String, String> baseAppErrorMessageMap) {
		this.baseAppErrorMessageMap = baseAppErrorMessageMap;
	}
   
 
}
